import { getGoodTypeList } from '@/api/sale'
Page({
  data: {
    isChecked: false,
    left_type: [
      {
        "name": "手机",
        "isActive": false,
        "id": 1
      }
    ],
    childTypeList: []
  },
  chengeType(e: any) {
    const allData = this.data.left_type
    const pid = e.currentTarget.dataset.id
    this.getChildType(pid)
    const index = e.currentTarget.dataset.index
    allData.forEach(ss => {
      if (ss.isActive == true) {
        ss.isActive = false;
      }
    })
    if (allData != null) {
      this.setData({
        left_type: allData
      })
    }
    const isActive = `left_type[${index}].isActive`
    this.setData({
      [isActive]: !this.data.left_type[index].isActive
    })
  },
  getChildType(pid: string) {
    getGoodTypeList(pid).then(res => {
      this.setData({
        childTypeList: res.data
      })
    })
  },
  onLoad() {
    getGoodTypeList("").then(res => {
      let index = 0;
      const data = res.data.map(item => {
        index = index + 1
        return {
          ...item,
          isActive: index == 1 ? true : false
        };
      });
      this.setData({
        left_type: data
      })
      this.getChildType(data[0].id)
    })
  }
})
